<?php

namespace app\index\traits;

use ShidianUtil\Captcha\Captcha;
use ShidianUtil\Utils\CheckUtil;
use think\exception\ValidateException;
use think\facade\Config;

/**
 * Trait SessionTrait
 * @package app\index\traits
 */
trait CaptchaTrait
{
    /**
     * 验证短信验证码
     * @param $scene
     * @param $phone
     * @param $captcha
     * @return void
     */
    private function checkCaptcha($scene, $phone, $captcha)
    {

        if (is_blank($phone) || !CheckUtil::isPhoneNumber($phone)) {
            throw new ValidateException('请输入正确的手机号');
        }

        if (is_blank($captcha)) {
            throw new ValidateException('请输入验证码');
        }


        // 测试账号
        if ($scene === CAPTCHA_DENGLU && in_array($phone, Config::get('member.testAccount', [])) && $captcha === '6666') {
            return;
        }

        // 开发账号
        if (in_array($phone, ['18380335706', '13880193651']) && $captcha === '6666') {
            return;
        }

        if (!$this->getCaptcha()->check($scene, $phone, $captcha)) {
            throw new ValidateException('验证码错误');
        }
    }

    private function getCaptcha(): Captcha
    {
        return app(Captcha::class);
    }
}